package com.huluobo.a2201mvvm_imdemo.shop;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2201mvvm_imdemo.mvvm.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by LC on 2024/4/8.
 * 观察者模式 3要素
 * 被观察者(Observable)
 * 观察者(Observer)
 * 订阅关系(subscribe)
 * 当被观察者发生改变时,会主动通知观察者,订阅关系就是他们之间的联系
 */
public class ShopRepo extends BaseRepo {
    public void getGoodsList(int categoryId, int currentPage, int pageSize, MutableLiveData<GoodsBeanResult> success,
                             MutableLiveData<String> failed) {
        apiService.getGoodsList(categoryId, currentPage, pageSize)//获取apiService中的请求方法
                .subscribeOn(Schedulers.io())//请求过程发生在io线程(子线程)
                .observeOn(AndroidSchedulers.mainThread())//请求到的返回发生在主线程
                .subscribe(new Observer<GoodsBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GoodsBeanResult goodsBeanResult) {
                        success.setValue(goodsBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
